package com.wk.warehouse.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wk.warehouse.entity.Auth;
import com.wk.warehouse.entity.Result;
import com.wk.warehouse.entity.User;
import com.wk.warehouse.mapper.AuthMapper;
import com.wk.warehouse.mapper.RoleMapper;
import com.wk.warehouse.mapper.UserMapper;
import com.wk.warehouse.service.UserService;
import com.wk.warehouse.util.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImp implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AuthMapper authMapper;
	@Autowired
	private RoleMapper roleMapper;
	/**
	 * 添加用户
	 */
	@Override
	public int addUser(User user) {
		// 密码进行加密处理
		user.setUserPwd(DigestUtil.hmacSign(user.getUserPwd()));
		return userMapper.addUser(user);
	}
	/**
	 * 删除用户
	 */
	@Override
	public int deleteUser(int userId) {
		return userMapper.deleteUser(userId);
	}

	/**
	 * 批量删除用户
	 */
	public int deleteUserList(List<Integer> userIds){
		int updateRows = 0;
		if(userIds!=null && userIds.size()>0){
			for(int userId: userIds){
				updateRows += userMapper.deleteUser(userId);
			}
		}
		return updateRows;
	}

	/**
	 * 根据用户名查找用户
	 */
	@Override
	public User findUserByCode(String userCode) {
		return userMapper.findUserByCode(userCode);
	}

	/**
	 * 修改用户信息
	 */
	@Override
	public int updateUser(User user) {
		return userMapper.updateUser(user);
	}

	/**
	 * 重置密码
	 */
	@Override
	public int updatePwd(int userId) {
		User user = new User();
		user.setUserId(userId);
		// 初始密码为123456
		user.setUserPwd(DigestUtil.hmacSign("123456"));
		return userMapper.updatePwd(user);
	}

	/**
	 * 修改用户状态
	 */
	@Override
	public int updateState(User user) {
		return userMapper.updateState(user);
	}

	/**
	 * 查询用户个数
	 */
	@Override
	public int selectCount(HashMap<String, Object> hashmap) {
		return userMapper.selectCount(hashmap);
	}

	/**
	 * 分页查询
	 */
	@Override
	public List<User> selectUserPage(HashMap<String, Object> map) {
		return userMapper.selectUserPage(map);
	}

	/**
	 * 分配角色
	 */
	@Override
	public int assignRoles(Integer userId, List<String> roleNameList) {
		// 删除已经存在的角色
		deleteRole(userId);
		// 重新赋予用户角色
		HashMap<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		int assignCount = 0;
		for(String roleName: roleNameList){
			int roleId = roleMapper.findRoleIdByName(roleName);
			map.put("roleId", roleId);
			userMapper.assignRoles(map);
			assignCount++;
		}
		return assignCount;
	}

	/**
	 * 删除已存在的角色
	 */
	@Override
	public int deleteRole(int userId) {
		return userMapper.deleteRole(userId);
	}
	/**
	 * 根据用户id查询权限
	 */
	@Override
	public String findAuthById(int userId) {
		return userMapper.findAuthById(userId);
	}
	/**
	 * 根据用户id删除权限
	 */
	@Override
	public int deleteAuthById(int userId) {
		return userMapper.deleteAuthById(userId);
	}
	/**
	 * 修改权限
	 */
	@Override
	public int grantAuth(int userId, List<Integer> authIds) {
		int updateRows = 0;
		userMapper.deleteAuthById(userId); // 删除用户的所有权限
		HashMap<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		for (int authId : authIds) {
			map.put("authId", authId);
			userMapper.insertUserAuth(map); // 添加用户权限
			updateRows++;
		}
		return updateRows;
	}
}
